package cn.com.pc.content.jpa;

import cn.com.pc.content.domain.Biz;

import javax.persistence.AttributeConverter;
import java.util.HashMap;
import java.util.Map;


public class BizConverter implements AttributeConverter<Biz, String> {
    final Map<String, Biz> map;

    public BizConverter() {
        map = new HashMap<>();
        for (Biz s : Biz.values()) {
            map.put(s.name(), s);
        }
    }

    @Override
    public String convertToDatabaseColumn(Biz attribute) {
        return attribute == null ? null : attribute.name();
    }

    @Override
    public Biz convertToEntityAttribute(String dbData) {
        return map.getOrDefault(dbData, null);
    }
}
